fix: log Anthropic system message first in span input#2109
Open
Joshua Tjhin (joshua-tj) wants to merge 1 commit into
Open
fix: log Anthropic system message first in span input#2109Joshua Tjhin (joshua-tj) wants to merge 1 commit into
Joshua Tjhin (joshua-tj) wants to merge 1 commit into
Conversation
The Anthropic instrumentation merges the top-level `system` param into the logged messages array, but appended it after the conversation messages. The model actually receives the system prompt first, and downstream consumers (e.g. opening a logged span in the playground) expect chat-message order, so a trailing system message had to be manually reordered before reuse. Prepend the system message instead, and update the e2e span-tree snapshots to match.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The Anthropic instrumentation merges the top-level
systemparam into the loggedinputmessages array (since the Anthropic API takes it separately rather than as a message). ButcoalesceInputappends it after the conversation messages, so everyanthropic.messages.createspan logs:[ { "role": "user", "content": "..." }, { "role": "system", "content": "..." } ]This doesn't match what the model actually receives (the API applies the system prompt first), and it makes logged calls awkward to reuse: opening a logged span in the playground requires manually reordering the messages before rerunning, since chat-message convention puts system first.
Fix
coalesceInputnow prepends the system message instead of appending it. Logged inputs read[system, user, ...].coalesceInputand added unit tests (system-first ordering, system as text-block array, no system prompt, no mutation of the caller's array) — it previously had no direct coverage.test:e2e:update; the diffs are purely the system message moving from last to first.Verification
js/core suite: 1070 passedtsc --noEmitand prettier cleanNote: this changes the logged
inputshape, so anything downstream that assumes the system message is last (e.g. saved playground sessions or scorers parsinginput) will see the new order. The in-repo e2e assertions already look up the system message by role rather than position, so none needed changes.🤖 Generated with Claude Code